clear all
set more off
set mem 2000m
set maxvar 30000


clear all
use "complete_main_new.dta", clear


merge 1:1 countryid year using "output/chris_predict_one_onset_int2_to15_alpha3_beta001_all_both.dta"
drop _merge
drop conflict

local 3 2
local 1 1

tsset countryid year



if "`3'" == "1" {

	quietly gen conflict = conflictpc
	
}

if "`3'" == "2" {

	quietly gen conflict = bdbest25
	
}

if "`3'" == "3" {
	

	quietly gen conflict = bdbest1000
	
}

                        
                        
generate one_before=conflict==0&F1.conflict==1
generate two_before=conflict==0&(F1.conflict==1|F2.conflict==1)

generate one_after=conflict==0&L1.conflict==1
generate two_after=conflict==0&(L1.conflict==1|L2.conflict==1)
	
	
if "`1'" == "." {
	replace one_before= 1 if conflict==1&F1.conflict==1
	replace two_before= 1 if conflict==1&(F1.conflict==1|F2.conflict==1)
}


*drop if _merge==2
*drop _merge
gen xconsthigh =xconst==7
gen xrcomphigh =xrcomp==3
gen xropenhigh =xropen==4
replace polity2 =0 if polity2==. & xconst!=.
tabulate democracy, gen(democ_)
generate armedconf4 = contig_bdbest25>3
replace armedconf4 = . if contig_bdbest25==.

generate lngdp = ln(rgdpl)
replace pop = ln(pop)


tsset countryid year   
generate growth = (rgdpl-L1.rgdpl)/L1.rgdpl



drop if avpop==.
drop if avpop<1000000



generate predicted = one_before==1& p_one_model21 >0.05
*tabulate p_one_model21
replace predicted =. if p_one_model21==.
generate one_before_predicted = predicted==0 & F1.predicted==1

tabulate country if predicted==1


bysort country: egen sample = max(predicted)
replace sample=. if conflict==.
tabulate sample

generate alarmed =  p_one_model21 >0.05
replace alarmed =. if  p_one_model21==.
*describe the sample
summarize alarmed predicted if  p_one_model21!=. & conflict==0


					
saveold "output/main_temp", replace

*2013
unzipfile thetas15_alpha3_beta001_all_both2013.dta.zip
use "thetas15_alpha3_beta001_all_both2013.dta", clear
erase thetas15_alpha3_beta001_all_both2013.dta

gen year = int(date/10000)

merge m:1 countryid year using "output/main_temp.dta"


			
rename theta0 industry
rename theta1 civlife
rename theta2 asia
rename theta3 sports
rename theta4 justice
rename theta5 tourism
rename theta6 politics
rename theta7 conflict1
rename theta8 business
rename theta9 economics
rename theta10 in_relations2
rename theta11 int_relations1
rename theta12 middleast
rename theta13 civlife2
rename theta14 conflict2



*top graph
graph drop _all
local bla FigI4

twoway  (kdensity economics  if p_one_model21 <0.05, lwidth(thick) lpattern(solid)  lcolor(black))  (kdensity economics  if p_one_model21 >0.05&p_one_model21!=., lwidth(thick) lpattern(dash)) if sample==1 & conflict==0 & economics<0.4, ///
subtitle("a) Minor decrease in economics shares", color(black)) ytitle(`"Kernel density"') xtitle(`"Share of economics topic in articles"') ylabel(none) legend( order(1 "low risk" 2 "high risk") size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(fig1) 

twoway  (kdensity conflict1  if p_one_model21 <0.05, lwidth(thick) lpattern(solid)  lcolor(black))  (kdensity conflict1  if p_one_model21 >0.05&p_one_model21!=., lwidth(thick) lpattern(dash)) if sample==1 & conflict==0&conflict1<0.4, ///
subtitle("b) Expansion in conflict shares", color(black)) ytitle(`"Kernel density"') xtitle(`"Share of first conflict topic in articles"') ylabel(none) legend( order(1 "Low risk" 2 "High risk") size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(fig2) 

*graph combine fig1 fig2 , title("", color(black)  size(small)) subtitle("", color(black) size(small)) plotregion(fcolor(white)) graphregion(fcolor(white)) 


twoway  (kdensity justice if p_one_model21 <0.05, lwidth(thick) lpattern(solid)  lcolor(black))  (kdensity justice  if p_one_model21 >0.05&p_one_model21!=., lwidth(thick) lpattern(dash)) if sample==1 & conflict==0 & justice <0.4, ///
subtitle("c) Decrease in justice shares", color(black)) ytitle(`"Kernel density"') xtitle(`"Share of justice topic in articles"') ylabel(none) legend( order(1 "low risk" 2 "high risk") size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(fig3) 

twoway  (kdensity conflict2  if p_one_model21 <0.05, lwidth(thick) lpattern(solid)  lcolor(black))  (kdensity conflict2  if p_one_model21 >0.05&p_one_model21!=., lwidth(thick) lpattern(dash)) if sample==1 & conflict==0&conflict2<0.4, ///
subtitle("d) Expansion in conflict shares", color(black)) ytitle(`"Kernel density"') xtitle(`"Share of second conflict topic in articles"') ylabel(none) legend( order(1 "Low risk" 2 "High risk") size(small)) ///
plotregion(fcolor(white)) graphregion(fcolor(white)) name(fig4) 

grc1leg fig1 fig2 fig3 fig4 , legendfrom(fig2) title("", color(black)  size(small)) subtitle("", color(black) size(small)) plotregion(fcolor(white)) graphregion(fcolor(white)) 

graph export output/`bla'.eps ,replace


